#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<cstring>
using namespace std;
using ll = long long;
const int N = 100010;
int n;
ll res = -1e18;
ll f[N], w[N];
int h[N], e[N], ne[N], idx;
void add(int a, int b)
{
    e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
void dfs(int u)
{
    f[u] = w[u];
    for (int i = h[u]; ~i; i = ne[i])
    {
        int j = e[i];
        dfs(j);
        f[u] += max(f[j], 0ll);
    }
    res = max(res, f[u]);
}
int main()
{
    cin >> n;
    memset(h, -1, sizeof h);
    for (int i = 1; i <= n; i++) cin >> w[i];
    for (int i = 1; i < n; i++)
    {
        int a, b;
        cin >> a >> b;
        if (a > b) swap(a, b);
        add(a, b);
    }
    dfs(1);
    cout << res << endl;
    return 0;
}